

<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>分类管理</span>
        <el-button
          style="float: right; padding: 3px 0"
          type="text"
          @click="addClasses"
          >添加分类</el-button
        >
      </div>
      <el-table :data="classes" style="width: 100%">
        <el-table-column prop="id" label="分类ID"> </el-table-column>
        <el-table-column prop="name" label="分类名"> </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="editClasses(scope.row)"
              type="warning"
              plain
              size="small"
              >编辑</el-button
            >
            <el-button
              type="danger"
              @click="deleteClasses(scope.row.id)"
              plain
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 对话框 -->
    <el-dialog
      :title="isAdd ? '添加分类' : '编辑分类'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="classe" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="classe.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqAllClasses,
  reqAddClasses,
  reqUpdateClasses,
  reqDeleteClasses,
} from "../api/index";

export default {
  name: "Classes",
  // data
  data() {
    return {
      classes: [],
      dialogVisible: false,
      classe: {},
      isAdd: false,
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
    };
  },
  // mounted
  mounted() {
    this.initClasses();
  },
  // methods
  methods: {
    // 请求所有分类
    async initClasses() {
      const { data } = await reqAllClasses();
      this.classes = data.items;
    },
    // 添加分类
    addClasses() {
      this.classe = {};
      this.isAdd = true;
      this.dialogVisible = true;
    },
    // 编辑分类
    editClasses(item) {
      this.isAdd = false;
      this.classe = { ...item };
      this.dialogVisible = true;
    },
    // 提交表单
    async toSubmit() {
      if (this.isAdd) {
        this.toAddClasses();
      } else {
        this.toUpdateClasses();
      }
    },
    // 发送添加分类请求
    async toAddClasses() {
      if (this.classe.name.trim()) {
        const { success, message } = await reqAddClasses(this.classe);
        if (success) {
          this.$message({
            type: "success",
            message,
          });
          this.initClasses();
          this.dialogVisible = false;
        } else {
          this.$message.error(message);
        }
      } else {
        this.$message("分类名称不能为空");
      }
    },
    // 发送更新分类请求
    async toUpdateClasses() {
      const { success, message } = await reqUpdateClasses(this.classe);
      if (success) {
        this.$message({
          type: "success",
          message,
        });
        this.initClasses();
        this.dialogVisible = false;
      } else {
        this.$message.error(message);
      }
    },
    // 删除分类
    deleteClasses(id) {
      this.$confirm("此操作将永久删除该分类, 是否继续?", "删除分类", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { success, message } = await reqDeleteClasses(id);
          if (success) {
            this.$message({
              type: "success",
              message,
            });
            this.initClasses();
          } else {
            this.$message.error(message);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleClose(done) {
      done();
      this.classe = {};
    },
  },
};
</script>

<style>
</style>